<%@ include file="shared_header.jsp" %>
<%@ page import="vrp.Planning" %>

<%
	SimpleDateFormat sdf = new SimpleDateFormat ("yyyy-MM-dd");
	PersistenceManager pm = PMF.get().getPersistenceManager();
	String query = "";
%>

<%
	String[] algorithmValue = {Integer.toString(0),Integer.toString(1)};
	String[] algorithmDisplay = {"Saving Heuristics","GRASP MetaHeuristics"};
	String[] distanceTypeValue = {Integer.toString(0),Integer.toString(1)};
	String[] distanceTypeDisplay = {"Driving Distance","Straight-line Distance"};

	String info = request.getParameter("info");
    if (info != null) {
    	if (info.equals("nodelivery")) {
%>
    		<div class="ui-state-highlight">There is no delivery on the selected date.</div>
<%
    	}
    }
%>
	<div style="padding: 0px 10px;">
		<h2>List of Plannings</h2>
		<table style="width: 100%;">
		
<%
	query = "select from " + Planning.class.getName();
    List<Planning> plannings = (List<Planning>) pm.newQuery(query).execute();
    if (plannings.isEmpty()) {
%>
			<tr><td>There is no planning in the database.</td></tr>
<%
    } else {
%>
			<tr>
				<th>Date</th>
				<th>Algorithm</th>
				<th>Distance Type</th>
				<th>Total Distance (m)</th>
				<th>Total Time (ms)</th>
				<th>Action</th>
			</tr>
<%
        for (Planning p : plannings) {
%>
			<tr>
				<td style="text-align: center;"><%= sdf.format(p.getDate()) %></td>
				<td style="text-align: center;"><%= writeSwitch(Integer.toString(p.getAlgorithm()),algorithmValue,algorithmDisplay) %></td>
				<td style="text-align: center;"><%= writeSwitch(Integer.toString(p.getDistanceType()),distanceTypeValue,distanceTypeDisplay) %></td>
				<td style="text-align: right;"><%= p.getTotalDistance() %></td>
				<td style="text-align: right;"><%= p.getTotalTime() %></td>
				<td style="text-align:center;">
					<form action="/planningroutes.jsp" method="get" target="_blank" >
						<input type="hidden" name="planning" value="<%= p.getKey().getId() %>"/>
						<input type="submit" value="View Routes" />
					</form>
					<input type="button" value="Delete" class="buttondelete"/>
				</td>
			</tr>
			<tr style="display: none;"><td colspan="5"></td></tr>
			<tr style="display: none;">
				<td colspan="6">
					<form action="/deleteplanning" method="post">
						<div><b>Delete Planning</b></div>
			    		<div>This action would delete the above planning permanently. Proceed?</div>
						<input type="hidden" name="key" value="<%= p.getKey().getId() %>"/>
						<input type="submit" value="Delete" />
						<input type="button" value="Cancel" class="buttoncancel" />
					</form>
				</td>
			</tr>
<%
        }
    }
%>
			
		</table>
		<input type="button" value="Add Planning" class="buttonadd" />
  		<div style="display: none;">
			<form action="/addplanning" method="post">
	    		<div>Date:&nbsp;<input type="text" name="date" class="datePicker" /></div>
	    		<div>Algorithm:&nbsp;<%= writeSelect("algorithm","",algorithmValue,algorithmDisplay) %></div>
	    		<div>Distance Type:&nbsp;<%= writeSelect("distanceType","",distanceTypeValue,distanceTypeDisplay) %></div>
	    		<div><input type="submit" value="Add" /><input type="button" value="Cancel" class="buttoncancel" /></div>
	  		</form>
	  	</div>
	</div>

<%
    pm.close();
%>

<%@ include file="shared_footer.jsp" %>